package com.github.benmanes.caffeine.demo.Population;

import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.concurrent.CompletableFuture;

public class DemoAsynchronousAuto {
    static System.Logger logger = System.getLogger(DemoAsynchronousAuto.class.getName());

    public static void hello(String[] args) {
        System.out.println("args = " + args);
    }


    public static void main(String... args) throws Exception {
        AsyncLoadingCache<String, String> cache = Caffeine.newBuilder().buildAsync(new CacheLoader<String, String>() {
            //加载数据
            @Nullable
            @Override
            public String load(@NonNull String key) throws Exception {
                System.out.println("loading  key=" + key);
                //e:  从 数据库去获取
                return "value from redis or db for " + key;
            }
        });


        String key = "疯狂创客圈";

        //会返回一个 future对象， 调用future对象的get方法会一直卡住直到得到返回，和多线程的submit一样
        CompletableFuture<String> future = cache.get(key);

        String val = future.get();
        System.out.println(val);

    }

}
